﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Sql;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
using Charities.Forms;
using System.Threading;
using System.Data.SqlClient;
using System.Data.SqlServerCe;

namespace Charities
{
    public partial class Login : Form
    {
        public Login()
        {
            Thread t = new Thread(new ThreadStart(AppSplsh));
            t.Start();
            Thread.Sleep(1100);
            InitializeComponent();
            t.Abort();
            Focus();
        }

        public Login(FormMDI rfm)
        {
            m = rfm;
            InitializeComponent();
            Focus();
        }
        ModelContainer ctx = new ModelContainer();
        public bool is_loaded = false;
        FormMDI m = null;
        public static string Hash(string value)
        {
            var sha = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] hash = sha.ComputeHash(Encoding.ASCII.GetBytes(value));
            return BytesToHex(hash).ToLower();
        }

        private static string BytesToHex(byte[] bytes)
        {
            return String.Concat(Array.ConvertAll(bytes, x => x.ToString("X2")));
        }
        private void btn_Ok_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection com = new SqlConnection();
                com.ConnectionString = ctx.Database.Connection.ConnectionString;
                if (com.ConnectionString.Contains("Initial"))
                {
                    com.Open();
                    com.Close();
                }
                else
                {
                    SqlCeConnection cecon = new SqlCeConnection();
                    cecon.ConnectionString = ctx.Database.Connection.ConnectionString;
                    cecon.Open();
                    cecon.Close();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("لايوجد اتصال بقاعدة البيانات يرجى التأكد من إعدادات الاتصال","خطأ في الاتصال بقاعدة البيانات",MessageBoxButtons.OK,MessageBoxIcon.Error);                
            }
            try
            {
                string pwd = Hash(txb_pwd.Text);
                User u = ctx.Users.Single(c => c.username == txb_username.Text && c.password == pwd);
                
                if (m != null && m.is_loaded)
                {
                    m.Dispose();
                    m = new FormMDI(u.Id);
                    m.Show();
                    this.Hide();
                }
                else
                {
                    m = new FormMDI(u.Id);
                    m.Show();
                    this.Hide();
                }
            }
            catch 
            {
                MessageBox.Show("خطأ في اسم المستخدم أو كلمة المرور");  
            } 
            
        }

        private void btn_Dispose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Login_Load(object sender, EventArgs e)
        {
            is_loaded = true;
        }
        public void AppSplsh()
        {
            Application.Run(new AppSplashScreenfrm());
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
            is_loaded = false;
        }

        private void Login_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
        }

        private void btnConnectionSettings_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show("تغيير قواعد الاتصال قد يحتاج لاعادة تشغيل البرنامج","تحذير",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (rs == DialogResult.Yes)
            {
                this.Hide();
                Forms.ConnetionBuilder con = new ConnetionBuilder();
                con.Show();
            }
        }
    }
}
